package com.example.mybaties.testone.aop.common;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class PerformanceMonitor {

    private static ThreadLocal<MethodPerformance> sThreadLocal = new ThreadLocal<>();

    /**
     * 启动对某个方法性能的监测
     */
    public static void begin(String methodName) {
        Logger logger = LogManager.getLogger(PerformanceMonitor.class);
        logger.debug("PerformanceMonitor...........begin()");
        MethodPerformance methodPerformance = new MethodPerformance(methodName);
        sThreadLocal.set(methodPerformance);

    }

    /**
     * 停止监测
     */
    public static void end() {
        Logger logger = LogManager.getLogger(PerformanceMonitor.class);
        logger.debug("PerformanceMonitor...........end()");
        MethodPerformance methodPerformance = sThreadLocal.get();
        if (methodPerformance != null) {
            methodPerformance.printPerformance();
        }
    }

}
